﻿// <copyright file="SampleData.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-05-05</date>
// <summary>SampleData class definition</summary>

namespace NYTimes.Core
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.IO;
    using System.Windows;

    /// <summary>
    /// New York Times Sample Data
    /// </summary>
    internal sealed class SampleData
    {
        /// <summary>
        /// Prevents a default instance of the SampleData class from being created
        /// </summary>
        private SampleData()
        {
        }

        /// <summary>
        /// Gets a value indicating whether the application is in design mode (In Blend or Visual Studio)
        /// </summary>
        public static bool IsInDesignTool
        {
            get
            {
#if SILVERLIGHT
                return DesignerProperties.IsInDesignTool;
#else
                if (Application.Current == null)
                {
                    return false;
                }

                return DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow);
#endif
            }
        }

        /// <summary>
        /// Gets a sample data stream or returns null if not found
        /// </summary>
        /// <param name="name">the name of the sample data</param>
        /// <returns>a string with the sample data or null if not found</returns>
        public static string GetSampleData(string name)
        {
            var streamName = string.Format(CultureInfo.InvariantCulture, "NYTimesSampleData;component/{0}", name);

            // Load sample data if this is not running on an HTML Page
            var streamResource = Application.GetResourceStream(
                new Uri(streamName, UriKind.Relative));

            if (streamResource == null)
            {
                return null;
            }

            using (StreamReader reader = new StreamReader(streamResource.Stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
